Change in openvpn[master]: Fix rare crash when read buffer are not initialised and read is still...
A message that is not in the archive (sent off-list or not yet received): gerrit.1768830991000.Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc@...2715...
plaisthos has uploaded this change for review. ( http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email )
Change subject: Fix rare crash when read buffer are not initialised and read is still signalled
......................................................................
Fix rare crash when read buffer are not initialised and read is still signalled
This assertion happens when we do not expect a read event from the socket and then in link_socket_read_tcp the function stream_buf_get_next can trigger an assert on ASSERT(buf_defined(&sb->next));
To avoid this weird corner case, just always initialise the read buffer whether or not we expect a read to occur.
Reproducing this bug requires very special circumstances. The reproduction is
openvpn --cert server-secp384r1.crt --key server-secp384r1.key \
--dev ml-tan --dev-type tap --tun-mtu 1400 --config ~/fp --topology subnet \
--port 2201 --verb 3 --mode server --tls-server --proto tcp6-server --ifconfig 10.0.0.1 255.255.255.0
as the server side and
openvpn --client --proto tcp --remote poseidon --port 2201 --dev tap
The client side must be on Linux. Other platform do not reproduce this bug.
Note that the client will not configure any IP or IPv6 on the interface and will also not bring up the interface. The server must also send at least one real data packet to the client (no keepalive ping). Just having the interface up normally produces enough traffic.
Now forcefully reset the TCP connection. E.g. by executing on the server
sudo ss --kill sport 2201
This will now trigger the assertion. This happens since OpenVPN waits forever to get a write back from the poll from the tun/tap device but this never happens since the device is not up.
As long as we do not get back the tun device for writing, we also do not put the socket back into the EVENT_READ state. And this also means that code to initialise the read buffer (stream_buf_set_next) is never run.
But the reset on the TCP socket triggers the TCP socket to be available for read, even if it is just for a read of 0 bytes to indicate the reset. So the link_socket_read_tcp will run into the assert.
---
M src/openvpn/socket.c
1 file changed, 5 insertions(+), 1 deletion(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/77/1477/1
src/openvpn/socket.c +5 -1
@@ -2927,7 +2927,11 @@ {if (s) { -if ((rwflags &EVENT_READ ) && !stream_buf_read_setup (s)) +/* Always setup the buffer to be able to get a read. There are corner cases +* where the socket select/poll returns a socket for read when we do not +* expect it, e.g. a read of len 0 when the socket receives a reset */ +const bool read_setup =stream_buf_read_setup (s); +if ((rwflags &EVENT_READ ) && !read_setup) {ASSERT (!persistent); rwflags &= ~EVENT_READ ;
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Attention is currently required from: plaisthos.
flichtenheld has posted comments on this change by plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email )
Change subject: Fix rare crash when read buffer are not initialised and read is still signalled
......................................................................
Patch Set 1: Code-Review-2
(3 comments)
Patchset:
PS1:t_client TCP tests fail across many platforms with this change
Commit Message:
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/1fc8d999_ff64750f?usp=email :
PS1, Line 26: The client side must be on Linux. Other platform do not reproduce this "platforms"
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/d0a1f917_c009ea97?usp=email :
PS1, Line 49: So the link_socket_read_tcp will run into the assert. Superfluous "the" before "link_socket_read_tcp"
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Comment-Date: Mon, 19 Jan 2026 14:11:45 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archiveAttention is currently required from: plaisthos.
Hello flichtenheld,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
to look at the new patch set (#2).
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Merge stream_buf_get_next and stream_buf_set_next
The stream_buf_set_next prepares a buffer in the stream_buf structure that will be retrieved by stream_buf_get the next time it is used.
This temporary copy of the buffer is unnecessary as the buffer next can also be constructed on the fly.
This also fixes a rare crash when read buffer are not initialised and read is still signalled as the initialisation of next will now happen whenever it is required.
This assertion happens when we do not expect a read event from the socket and then in link_socket_read_tcp the function stream_buf_get_next can trigger an assert on ASSERT(buf_defined(&sb->next));
To avoid this weird corner case, just always initialise the read buffer whether or not we expect a read to occur.
This also adds documentation about the methods and field associated with the stream_buf structure.
Reproducing this bug requires very special circumstances. The reproduction is
openvpn --cert server-secp384r1.crt --key server-secp384r1.key \
--dev ml-tan --dev-type tap --tun-mtu 1400 --config ~/fp --topology subnet \
--port 2201 --verb 3 --mode server --tls-server --proto tcp6-server --ifconfig 10.0.0.1 255.255.255.0
as the server side and
openvpn --client --proto tcp --remote poseidon --port 2201 --dev tap
The client side must be on Linux. Other platform do not reproduce this bug.
Note that the client will not configure any IP or IPv6 on the interface and will also not bring up the interface. The server must also send at least one real data packet to the client (no keepalive ping). Just having the interface up normally produces enough traffic.
Now forcefully reset the TCP connection. E.g. by executing on the server
sudo ss --kill sport 2201
This will now trigger the assertion. This happens since OpenVPN waits forever to get a write back from the poll from the tun/tap device but this never happens since the device is not up.
As long as we do not get back the tun device for writing, we also do not put the socket back into the EVENT_READ state. And this also means that code to initialise the read buffer (stream_buf_set_next) is never run.
But the reset on the TCP socket triggers the TCP socket to be available for read, even if it is just for a read of 0 bytes to indicate the reset. So the link_socket_read_tcp will run into the assert.
Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
---
M src/openvpn/socket.c
M src/openvpn/socket.h
2 files changed, 83 insertions(+), 39 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/77/1477/2
src/openvpn/socket.c +51 -30
@@ -44,9 +44,7 @@bool sockets_read_residual (const struct context *c) { -int i; - -for (i =0 ; i < c->c1.link_sockets_num; i++) +for (int i =0 ; i < c->c1.link_sockets_num; i++) {if (c->c2.link_sockets[i]->stream_buf.residual_fully_formed) {
@@ -2065,13 +2063,19 @@* stream connection. */ +/** +* resets the stream buffer to be setup for the next round of +* reassembling a packet +* +* But still leaves the current packet in sb->buf to be potentially +* read. +*/ static inline void stream_buf_reset (struct stream_buf *sb) {dmsg (D_STREAM_DEBUG ,"STREAM: RESET" ); sb->residual_fully_formed = false; sb->buf = sb->buf_init; -buf_reset (&sb->next); sb->len =-1 ; }
@@ -2093,19 +2097,35 @@dmsg (D_STREAM_DEBUG ,"STREAM: INIT maxlen=%d" , sb->maxlen); } -static inline void -stream_buf_set_next (struct stream_buf *sb) +/** +* Return a buffer that is backed by the same backend as sb->buf that +* determines where the next read should be done by also having the +* right offset into sb->buf +* @param sb the stream buffer from which to construct the next buffer +*/ +static inline struct buffer +stream_buf_get_next (struct stream_buf *sb) {/* set up 'next' for next i/o read */ - sb->next = sb->buf; - sb->next.offset = sb->buf.offset + sb->buf.len; - sb->next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; -dmsg (D_STREAM_DEBUG ,"STREAM: SET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , - sb->buf.offset, sb->buf.len, sb->next.offset, sb->next.len, sb->len, sb->maxlen); -ASSERT (sb->next.len >0 ); -ASSERT (buf_safe (&sb->buf, sb->next.len)); +struct buffer next; + next = sb->buf; + next.offset = sb->buf.offset + sb->buf.len; + next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; +dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , + sb->buf.offset, sb->buf.len, next.offset, next.len, sb->len, sb->maxlen); +ASSERT (next.len >0 ); +ASSERT (buf_safe (&sb->buf, next.len)); +return next; } +/** +* Sets the parameter buf to the current buffer of sb->buf. +* This function assumes that caller already checked if the packet in sb->buf +* is fully assembled. +* +* @param sb stream buffer to operate on +* @param buf buffer to point to the contents of buf +*/ static inline void stream_buf_get_final (struct stream_buf *sb,struct buffer *buf) {
@@ -2114,33 +2134,36 @@ *buf = sb->buf; } -static inline void -stream_buf_get_next (struct stream_buf *sb,struct buffer *buf) -{ -dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT len=%d" ,buf_defined (&sb->next) ? sb->next.len :-1 ); -ASSERT (buf_defined (&sb->next)); - *buf = sb->next; -} -bool stream_buf_read_setup_dowork (struct stream_buf *sb) {if (sb->residual.len && !sb->residual_fully_formed) { -ASSERT (buf_copy (sb->buf, &sock->stream_buf.residual)); +ASSERT (buf_copy (&sb->buf, &sb->residual));ASSERT (buf_init (&sb->residual,0 )); sb->residual_fully_formed =stream_buf_added (sb,0 );dmsg (D_STREAM_DEBUG ,"STREAM: RESIDUAL FULLY FORMED [%s], len=%d" , - stream_buf->residual_fully_formed ?"YES" :"NO" , sb->residual.len); + sb->residual_fully_formed ?"YES" :"NO" , sb->residual.len); } -if (!sb->residual_fully_formed) - { -stream_buf_set_next (sb); - }return !sb->residual_fully_formed; } +/** +* This will determine if sb->buf contains a full packet. It will also +* move anything in sb->buf beyond a full beyond packet to sb->residual +* +* If this is the first time the function is called with a valid buffer +* and port sharing is enabled the function will also determine if this +* is OpenVPN protocol or not and store the result in sb->port_share_state. +* +* If a packet is outside of the allowed range is detected, the error state +* on sb is set. +* +* @param sb the stream buffer +* @param length_added The length that has been added to sb->buf +* @return true if sb->buf contains fully reassembled packet +*/ static bool stream_buf_added (struct stream_buf *sb,int length_added) {
@@ -2203,7 +2226,6 @@else {dmsg (D_STREAM_DEBUG ,"STREAM: ADD returned FALSE (have=%d need=%d)" , sb->buf.len, sb->len); -stream_buf_set_next (sb);return false; } }
@@ -2273,8 +2295,7 @@sockethandle_t sh = { .s = sock->sd }; len =sockethandle_finalize (sh, &sock->reads, buf,NULL );#else -struct buffer frag; -stream_buf_get_next (&sock->stream_buf, &frag); +struct buffer frag =stream_buf_get_next (&sock->stream_buf); len =recv (sock->sd,BPTR (&frag),BLEN (&frag),MSG_NOSIGNAL );#endif
src/openvpn/socket.h +32 -9
64 lines not on this page · raw message
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 2
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archiveAttention is currently required from: plaisthos.
Hello flichtenheld,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
to look at the new patch set (#3).
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Merge stream_buf_get_next and stream_buf_set_next
The stream_buf_set_next prepares a buffer in the stream_buf structure that will be retrieved by stream_buf_get the next time it is used.
This temporary copy of the buffer is unnecessary as the buffer next can also be constructed on the fly.
This also fixes a rare crash when read buffer are not initialised and read is still signalled as the initialisation of next will now happen whenever it is required.
This assertion happens when we do not expect a read event from the socket and then in link_socket_read_tcp the function stream_buf_get_next can trigger an assert on ASSERT(buf_defined(&sb->next));
To avoid this weird corner case, just always initialise the read buffer whether or not we expect a read to occur.
This also adds documentation about the methods and field associated with the stream_buf structure.
Reproducing this bug requires very special circumstances. The reproduction is
openvpn --cert server-secp384r1.crt --key server-secp384r1.key \
--dev ml-tan --dev-type tap --tun-mtu 1400 --config ~/fp --topology subnet \
--port 2201 --verb 3 --mode server --tls-server --proto tcp6-server --ifconfig 10.0.0.1 255.255.255.0
as the server side and
openvpn --client --proto tcp --remote poseidon --port 2201 --dev tap
The client side must be on Linux. Other platform do not reproduce this bug.
Note that the client will not configure any IP or IPv6 on the interface and will also not bring up the interface. The server must also send at least one real data packet to the client (no keepalive ping). Just having the interface up normally produces enough traffic.
Now forcefully reset the TCP connection. E.g. by executing on the server
sudo ss --kill sport 2201
This will now trigger the assertion. This happens since OpenVPN waits forever to get a write back from the poll from the tun/tap device but this never happens since the device is not up.
As long as we do not get back the tun device for writing, we also do not put the socket back into the EVENT_READ state. And this also means that code to initialise the read buffer (stream_buf_set_next) is never run.
But the reset on the TCP socket triggers the TCP socket to be available for read, even if it is just for a read of 0 bytes to indicate the reset. So the link_socket_read_tcp will run into the assert.
Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
---
M src/openvpn/socket.c
M src/openvpn/socket.h
2 files changed, 84 insertions(+), 40 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/77/1477/3
src/openvpn/socket.c +52 -31
@@ -44,9 +44,7 @@bool sockets_read_residual (const struct context *c) { -int i; - -for (i =0 ; i < c->c1.link_sockets_num; i++) +for (int i =0 ; i < c->c1.link_sockets_num; i++) {if (c->c2.link_sockets[i]->stream_buf.residual_fully_formed) {
@@ -2065,13 +2063,19 @@* stream connection. */ +/** +* resets the stream buffer to be setup for the next round of +* reassembling a packet +* +* But still leaves the current packet in sb->buf to be potentially +* read. +*/ static inline void stream_buf_reset (struct stream_buf *sb) {dmsg (D_STREAM_DEBUG ,"STREAM: RESET" ); sb->residual_fully_formed = false; sb->buf = sb->buf_init; -buf_reset (&sb->next); sb->len =-1 ; }
@@ -2093,19 +2097,35 @@dmsg (D_STREAM_DEBUG ,"STREAM: INIT maxlen=%d" , sb->maxlen); } -static inline void -stream_buf_set_next (struct stream_buf *sb) +/** +* Return a buffer that is backed by the same backend as sb->buf that +* determines where the next read should be done by also having the +* right offset into sb->buf +* @param sb the stream buffer from which to construct the next buffer +*/ +static inline struct buffer +stream_buf_get_next (struct stream_buf *sb) {/* set up 'next' for next i/o read */ - sb->next = sb->buf; - sb->next.offset = sb->buf.offset + sb->buf.len; - sb->next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; -dmsg (D_STREAM_DEBUG ,"STREAM: SET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , - sb->buf.offset, sb->buf.len, sb->next.offset, sb->next.len, sb->len, sb->maxlen); -ASSERT (sb->next.len >0 ); -ASSERT (buf_safe (&sb->buf, sb->next.len)); +struct buffer next; + next = sb->buf; + next.offset = sb->buf.offset + sb->buf.len; + next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; +dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , + sb->buf.offset, sb->buf.len, next.offset, next.len, sb->len, sb->maxlen); +ASSERT (next.len >0 ); +ASSERT (buf_safe (&sb->buf, next.len)); +return next; } +/** +* Sets the parameter buf to the current buffer of sb->buf. +* This function assumes that caller already checked if the packet in sb->buf +* is fully assembled. +* +* @param sb stream buffer to operate on +* @param buf buffer to point to the contents of buf +*/ static inline void stream_buf_get_final (struct stream_buf *sb,struct buffer *buf) {
@@ -2114,33 +2134,36 @@ *buf = sb->buf; } -static inline void -stream_buf_get_next (struct stream_buf *sb,struct buffer *buf) -{ -dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT len=%d" ,buf_defined (&sb->next) ? sb->next.len :-1 ); -ASSERT (buf_defined (&sb->next)); - *buf = sb->next; -} -bool stream_buf_read_setup_dowork (struct stream_buf *sb) {if (sb->residual.len && !sb->residual_fully_formed) { -ASSERT (buf_copy (sb->buf, &sock->stream_buf.residual)); +ASSERT (buf_copy (&sb->buf, &sb->residual));ASSERT (buf_init (&sb->residual,0 )); sb->residual_fully_formed =stream_buf_added (sb,0 );dmsg (D_STREAM_DEBUG ,"STREAM: RESIDUAL FULLY FORMED [%s], len=%d" , - stream_buf->residual_fully_formed ?"YES" :"NO" , sb->residual.len); + sb->residual_fully_formed ?"YES" :"NO" , sb->residual.len); } -if (!sb->residual_fully_formed) - { -stream_buf_set_next (sb); - }return !sb->residual_fully_formed; } +/** +* This will determine if sb->buf contains a full packet. It will also +* move anything in sb->buf beyond a full beyond packet to sb->residual +* +* If this is the first time the function is called with a valid buffer +* and port sharing is enabled the function will also determine if this +* is OpenVPN protocol or not and store the result in sb->port_share_state. +* +* If a packet is outside of the allowed range is detected, the error state +* on sb is set. +* +* @param sb the stream buffer +* @param length_added The length that has been added to sb->buf +* @return true if sb->buf contains fully reassembled packet +*/ static bool stream_buf_added (struct stream_buf *sb,int length_added) {
@@ -2203,7 +2226,6 @@else {dmsg (D_STREAM_DEBUG ,"STREAM: ADD returned FALSE (have=%d need=%d)" , sb->buf.len, sb->len); -stream_buf_set_next (sb);return false; } }
@@ -2273,8 +2295,7 @@sockethandle_t sh = { .s = sock->sd }; len =sockethandle_finalize (sh, &sock->reads, buf,NULL );#else -struct buffer frag; -stream_buf_get_next (&sock->stream_buf, &frag); +struct buffer frag =stream_buf_get_next (&sock->stream_buf); len =recv (sock->sd,BPTR (&frag),BLEN (&frag),MSG_NOSIGNAL );#endif
@@ -2550,7 +2571,7 @@ }else if (proto_is_tcp (sock->info.proto)) { -stream_buf_get_next (&sock->stream_buf, &sock->reads.buf); + sock->reads.buf =stream_buf_get_next (&sock->stream_buf); }else {
src/openvpn/socket.h +32 -9
64 lines not on this page · raw message
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 3
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archiveAttention is currently required from: plaisthos.
flichtenheld has posted comments on this change by plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email )
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Patch Set 3: -Code-Review
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 3
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Comment-Date: Wed, 21 Jan 2026 11:58:04 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archiveAttention is currently required from: plaisthos.
flichtenheld has posted comments on this change by plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email )
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Patch Set 3: Code-Review-1
(6 comments)
File src/openvpn/socket.h:
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/c97b5e83_9e9c469d?usp=email :
PS3, Line 552: * required and other false. (full packet is ready) "otherwise"?
File src/openvpn/socket.c:
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/487937f5_d30dfd28?usp=email :
PS3, Line 2067: * resets the stream buffer to be setup for the next round of "set up"
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/5b1e950d_159c415b?usp=email :
PS3, Line 2122: * Sets the parameter buf to the current buffer of sb->buf. superfluous whitespace in the middle of the sentence
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/7ccb4681_dd24406d?usp=email :
PS3, Line 2154: * move anything in sb->buf beyond a full beyond packet to sb->residual Remove second "beyond"
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/b1bcaa05_4b1c487c?usp=email :
PS3, Line 2160: * If a packet is outside of the allowed range is detected, the error state Remove first "is"
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/73e1c3bb_381e01d3?usp=email :
PS3, Line 2164: * @param length_added The length that has been added to sb->buf
Would be good to add a little more information here. I see this can be 0. Is that for the initial call? When do we call this with non-zero length_added?
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 3
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Comment-Date: Wed, 21 Jan 2026 12:14:08 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archiveAttention is currently required from: plaisthos.
flichtenheld has posted comments on this change by plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email )
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Patch Set 3:
(1 comment)
File src/openvpn/socket.c:
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/ed6fb644_c5d3eb95?usp=email :
PS3, Line 2070: * But still leaves the current packet in sb->buf to be potentially
Will not report all individual occurrences but all instances of something like this could use a \c or \p so that this is formatted as code. Would make it easier to read I think.
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 3
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Comment-Date: Wed, 21 Jan 2026 12:24:46 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archiveAttention is currently required from: flichtenheld.
plaisthos has posted comments on this change by plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email )
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Patch Set 3:
(6 comments)
File src/openvpn/socket.h:
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/f7398ccd_99a35101?usp=email :
PS3, Line 552: * required and other false. (full packet is ready)
Quoted 1 line
"otherwise"?
Done
File src/openvpn/socket.c:
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/39240118_2bfb6057?usp=email :
PS3, Line 2067: * resets the stream buffer to be setup for the next round of
Quoted 1 line
"set up"
Done
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/40e9a444_87973fac?usp=email :
PS3, Line 2122: * Sets the parameter buf to the current buffer of sb->buf.
Quoted 1 line
superfluous whitespace in the middle of the sentence
Done
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/a122986d_eed4a789?usp=email :
PS3, Line 2154: * move anything in sb->buf beyond a full beyond packet to sb->residual
Quoted 1 line
Remove second "beyond"
Done
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/3f3c7b97_17270177?usp=email :
PS3, Line 2160: * If a packet is outside of the allowed range is detected, the error state
Quoted 1 line
Remove first "is"
Done
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/ee70cc87_34f67587?usp=email :
PS3, Line 2164: * @param length_added The length that has been added to sb->buf
Quoted 1 line
Would be good to add a little more information here. I see this can be 0. […]
Added a paragraph for that.
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 3
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Thu, 22 Jan 2026 09:10:50 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: flichtenheld <[email protected]...>
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archiveAttention is currently required from: flichtenheld.
Hello flichtenheld,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
to look at the new patch set (#4).
The following approvals got outdated and were removed:
Code-Review-1 by flichtenheld
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Merge stream_buf_get_next and stream_buf_set_next
The stream_buf_set_next prepares a buffer in the stream_buf structure that will be retrieved by stream_buf_get the next time it is used.
This temporary copy of the buffer is unnecessary as the buffer next can also be constructed on the fly.
This also fixes a rare crash when read buffer are not initialised and read is still signalled as the initialisation of next will now happen whenever it is required.
This assertion happens when we do not expect a read event from the socket and then in link_socket_read_tcp the function stream_buf_get_next can trigger an assert on ASSERT(buf_defined(&sb->next));
To avoid this weird corner case, just always initialise the read buffer whether or not we expect a read to occur.
This also adds documentation about the methods and field associated with the stream_buf structure.
Reproducing this bug requires very special circumstances. The reproduction is
openvpn --cert server-secp384r1.crt --key server-secp384r1.key \
--dev ml-tan --dev-type tap --tun-mtu 1400 --config ~/fp --topology subnet \
--port 2201 --verb 3 --mode server --tls-server --proto tcp6-server --ifconfig 10.0.0.1 255.255.255.0
as the server side and
openvpn --client --proto tcp --remote poseidon --port 2201 --dev tap
The client side must be on Linux. Other platform do not reproduce this bug.
Note that the client will not configure any IP or IPv6 on the interface and will also not bring up the interface. The server must also send at least one real data packet to the client (no keepalive ping). Just having the interface up normally produces enough traffic.
Now forcefully reset the TCP connection. E.g. by executing on the server
sudo ss --kill sport 2201
This will now trigger the assertion. This happens since OpenVPN waits forever to get a write back from the poll from the tun/tap device but this never happens since the device is not up.
As long as we do not get back the tun device for writing, we also do not put the socket back into the EVENT_READ state. And this also means that code to initialise the read buffer (stream_buf_set_next) is never run.
But the reset on the TCP socket triggers the TCP socket to be available for read, even if it is just for a read of 0 bytes to indicate the reset. So the link_socket_read_tcp will run into the assert.
Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
---
M src/openvpn/socket.c
M src/openvpn/socket.h
2 files changed, 90 insertions(+), 40 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/77/1477/4
src/openvpn/socket.c +58 -31
@@ -44,9 +44,7 @@bool sockets_read_residual (const struct context *c) { -int i; - -for (i =0 ; i < c->c1.link_sockets_num; i++) +for (int i =0 ; i < c->c1.link_sockets_num; i++) {if (c->c2.link_sockets[i]->stream_buf.residual_fully_formed) {
@@ -2065,13 +2063,19 @@* stream connection. */ +/** +* resets the stream buffer to be set up for the next round of +* reassembling a packet +* +* But still leaves the current packet in \c sb->buf to be potentially +* read. +*/ static inline void stream_buf_reset (struct stream_buf *sb) {dmsg (D_STREAM_DEBUG ,"STREAM: RESET" ); sb->residual_fully_formed = false; sb->buf = sb->buf_init; -buf_reset (&sb->next); sb->len =-1 ; }
@@ -2093,19 +2097,35 @@dmsg (D_STREAM_DEBUG ,"STREAM: INIT maxlen=%d" , sb->maxlen); } -static inline void -stream_buf_set_next (struct stream_buf *sb) +/** +* Return a buffer that is backed by the same backend as sb->buf that +* determines where the next read should be done by also having the +* right offset into \c sb->buf. +* @param sb the stream buffer from which to construct the next buffer +*/ +static inline struct buffer +stream_buf_get_next (struct stream_buf *sb) {/* set up 'next' for next i/o read */ - sb->next = sb->buf; - sb->next.offset = sb->buf.offset + sb->buf.len; - sb->next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; -dmsg (D_STREAM_DEBUG ,"STREAM: SET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , - sb->buf.offset, sb->buf.len, sb->next.offset, sb->next.len, sb->len, sb->maxlen); -ASSERT (sb->next.len >0 ); -ASSERT (buf_safe (&sb->buf, sb->next.len)); +struct buffer next; + next = sb->buf; + next.offset = sb->buf.offset + sb->buf.len; + next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; +dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , + sb->buf.offset, sb->buf.len, next.offset, next.len, sb->len, sb->maxlen); +ASSERT (next.len >0 ); +ASSERT (buf_safe (&sb->buf, next.len)); +return next; } +/** +* Sets the parameter buf to the current buffer of \c sb->buf. +* This function assumes that caller already checked if the packet in \c sb->buf +* is fully assembled. +* +* @param sb stream buffer to operate on +* @param buf buffer to point to the contents of buf +*/ static inline void stream_buf_get_final (struct stream_buf *sb,struct buffer *buf) {
@@ -2114,33 +2134,42 @@ *buf = sb->buf; } -static inline void -stream_buf_get_next (struct stream_buf *sb,struct buffer *buf) -{ -dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT len=%d" ,buf_defined (&sb->next) ? sb->next.len :-1 ); -ASSERT (buf_defined (&sb->next)); - *buf = sb->next; -} -bool stream_buf_read_setup_dowork (struct stream_buf *sb) {if (sb->residual.len && !sb->residual_fully_formed) { -ASSERT (buf_copy (sb->buf, &sock->stream_buf.residual)); +ASSERT (buf_copy (&sb->buf, &sb->residual));ASSERT (buf_init (&sb->residual,0 )); sb->residual_fully_formed =stream_buf_added (sb,0 );dmsg (D_STREAM_DEBUG ,"STREAM: RESIDUAL FULLY FORMED [%s], len=%d" , - stream_buf->residual_fully_formed ?"YES" :"NO" , sb->residual.len); + sb->residual_fully_formed ?"YES" :"NO" , sb->residual.len); } -if (!sb->residual_fully_formed) - { -stream_buf_set_next (sb); - }return !sb->residual_fully_formed; } +/** +* This will determine if \c sb->buf contains a full packet. It will also +* move anything in \c sb->buf beyond a full packet to \c sb->residual. +* +* The first time the function is called with a valid buffer and port sharing +* is enabled, the function will also determine if the buffer contains +* OpenVPN protocol data and store the result in \c sb->port_share_state. +* +* If a packet outside the allowed range is detected, the error state +* on \c sb is set. +* +* Since the buffer in \c sb->buf is modified from the outside (via +* \c stream_buf_get_next) the parameter \p length_added needs to be set +* to the amount of bytes that have been written to this buffer. If the +* buffer was not modified but should still be analysed and potentially +* split to \c sb->residual, the parameter \p length_added should be 0. +* +* @param sb the stream buffer +* @param length_added The length that has been added to \c sb->buf +* @return true if \c sb->buf contains fully reassembled packet +*/ static bool stream_buf_added (struct stream_buf *sb,int length_added) {
@@ -2203,7 +2232,6 @@else {dmsg (D_STREAM_DEBUG ,"STREAM: ADD returned FALSE (have=%d need=%d)" , sb->buf.len, sb->len); -stream_buf_set_next (sb);return false; } }
@@ -2273,8 +2301,7 @@sockethandle_t sh = { .s = sock->sd }; len =sockethandle_finalize (sh, &sock->reads, buf,NULL );#else -struct buffer frag; -stream_buf_get_next (&sock->stream_buf, &frag); +struct buffer frag =stream_buf_get_next (&sock->stream_buf); len =recv (sock->sd,BPTR (&frag),BLEN (&frag),MSG_NOSIGNAL );#endif
@@ -2550,7 +2577,7 @@ }else if (proto_is_tcp (sock->info.proto)) { -stream_buf_get_next (&sock->stream_buf, &sock->reads.buf); + sock->reads.buf =stream_buf_get_next (&sock->stream_buf); }else {
src/openvpn/socket.h +32 -9
64 lines not on this page · raw message
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 4
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archiveAttention is currently required from: plaisthos.
flichtenheld has posted comments on this change by plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email )
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Patch Set 4: Code-Review-1
(3 comments)
Patchset:
PS4:
You didn't fix the comments in the commit message.
File src/openvpn/socket.c:
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/015184f8_a171efb7?usp=email :
PS3, Line 2070: * But still leaves the current packet in sb->buf to be potentially
Quoted 1 line
Will not report all individual occurrences but all instances of something like this could use a
\c[…]
Done
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/e279cd9f_40cac0d3?usp=email :
PS3, Line 2164: * @param length_added The length that has been added to sb->buf
Quoted 1 line
Added a paragraph for that.
Done
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 4
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Comment-Date: Thu, 22 Jan 2026 12:05:06 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Comment-In-Reply-To: plaisthos <[email protected]...>
Comment-In-Reply-To: flichtenheld <[email protected]...>
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archiveAttention is currently required from: flichtenheld.
plaisthos has posted comments on this change by plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email )
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Patch Set 4:
(2 comments)
Commit Message:
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/36ef9656_74597bff?usp=email :
PS1, Line 26: The client side must be on Linux. Other platform do not reproduce this
Quoted 1 line
"platforms"
Done
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/97f3bd95_9020ab31?usp=email :
PS1, Line 49: So the link_socket_read_tcp will run into the assert.
Quoted 1 line
Superfluous "the" before "
link_socket_read_tcp"
Done
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 4
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Thu, 22 Jan 2026 12:40:03 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: flichtenheld <[email protected]...>
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archiveAttention is currently required from: flichtenheld.
Hello flichtenheld,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
to look at the new patch set (#5).
The following approvals got outdated and were removed:
Code-Review-1 by flichtenheld
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Merge stream_buf_get_next and stream_buf_set_next
The stream_buf_set_next prepares a buffer in the stream_buf structure that will be retrieved by stream_buf_get the next time it is used.
This temporary copy of the buffer is unnecessary as the buffer next can also be constructed on the fly.
This also fixes a rare crash when read buffer are not initialised and read is still signalled as the initialisation of next will now happen whenever it is required.
This assertion happens when we do not expect a read event from the socket and then in link_socket_read_tcp the function stream_buf_get_next can trigger an assert on ASSERT(buf_defined(&sb->next));
To avoid this weird corner case, just always initialise the read buffer whether or not we expect a read to occur.
This also adds documentation about the methods and field associated with the stream_buf structure.
Reproducing this bug requires very special circumstances. The reproduction is
openvpn --cert server-secp384r1.crt --key server-secp384r1.key \
--dev ml-tan --dev-type tap --tun-mtu 1400 --config ~/fp --topology subnet \
--port 2201 --verb 3 --mode server --tls-server --proto tcp6-server --ifconfig 10.0.0.1 255.255.255.0
as the server side and
openvpn --client --proto tcp --remote poseidon --port 2201 --dev tap
The client side must be on Linux. Other platforms do not reproduce this bug.
Note that the client will not configure any IP or IPv6 on the interface and will also not bring up the interface. The server must also send at least one real data packet to the client (no keepalive ping). Just having the interface up normally produces enough traffic.
Now forcefully reset the TCP connection. E.g. by executing on the server
sudo ss --kill sport 2201
This will now trigger the assertion. This happens since OpenVPN waits forever to get a write back from the poll from the tun/tap device but this never happens since the device is not up.
As long as we do not get back the tun device for writing, we also do not put the socket back into the EVENT_READ state. And this also means that code to initialise the read buffer (stream_buf_set_next) is never run.
But the reset on the TCP socket triggers the TCP socket to be available for read, even if it is just for a read of 0 bytes to indicate the reset. So the function link_socket_read_tcp will run into the assert.
Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
---
M src/openvpn/socket.c
M src/openvpn/socket.h
2 files changed, 90 insertions(+), 40 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/77/1477/5
src/openvpn/socket.c +58 -31
@@ -44,9 +44,7 @@bool sockets_read_residual (const struct context *c) { -int i; - -for (i =0 ; i < c->c1.link_sockets_num; i++) +for (int i =0 ; i < c->c1.link_sockets_num; i++) {if (c->c2.link_sockets[i]->stream_buf.residual_fully_formed) {
@@ -2065,13 +2063,19 @@* stream connection. */ +/** +* resets the stream buffer to be set up for the next round of +* reassembling a packet +* +* But still leaves the current packet in \c sb->buf to be potentially +* read. +*/ static inline void stream_buf_reset (struct stream_buf *sb) {dmsg (D_STREAM_DEBUG ,"STREAM: RESET" ); sb->residual_fully_formed = false; sb->buf = sb->buf_init; -buf_reset (&sb->next); sb->len =-1 ; }
@@ -2093,19 +2097,35 @@dmsg (D_STREAM_DEBUG ,"STREAM: INIT maxlen=%d" , sb->maxlen); } -static inline void -stream_buf_set_next (struct stream_buf *sb) +/** +* Return a buffer that is backed by the same backend as sb->buf that +* determines where the next read should be done by also having the +* right offset into \c sb->buf. +* @param sb the stream buffer from which to construct the next buffer +*/ +static inline struct buffer +stream_buf_get_next (struct stream_buf *sb) {/* set up 'next' for next i/o read */ - sb->next = sb->buf; - sb->next.offset = sb->buf.offset + sb->buf.len; - sb->next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; -dmsg (D_STREAM_DEBUG ,"STREAM: SET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , - sb->buf.offset, sb->buf.len, sb->next.offset, sb->next.len, sb->len, sb->maxlen); -ASSERT (sb->next.len >0 ); -ASSERT (buf_safe (&sb->buf, sb->next.len)); +struct buffer next; + next = sb->buf; + next.offset = sb->buf.offset + sb->buf.len; + next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; +dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , + sb->buf.offset, sb->buf.len, next.offset, next.len, sb->len, sb->maxlen); +ASSERT (next.len >0 ); +ASSERT (buf_safe (&sb->buf, next.len)); +return next; } +/** +* Sets the parameter buf to the current buffer of \c sb->buf. +* This function assumes that caller already checked if the packet in \c sb->buf +* is fully assembled. +* +* @param sb stream buffer to operate on +* @param buf buffer to point to the contents of buf +*/ static inline void stream_buf_get_final (struct stream_buf *sb,struct buffer *buf) {
@@ -2114,33 +2134,42 @@ *buf = sb->buf; } -static inline void -stream_buf_get_next (struct stream_buf *sb,struct buffer *buf) -{ -dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT len=%d" ,buf_defined (&sb->next) ? sb->next.len :-1 ); -ASSERT (buf_defined (&sb->next)); - *buf = sb->next; -} -bool stream_buf_read_setup_dowork (struct stream_buf *sb) {if (sb->residual.len && !sb->residual_fully_formed) { -ASSERT (buf_copy (sb->buf, &sock->stream_buf.residual)); +ASSERT (buf_copy (&sb->buf, &sb->residual));ASSERT (buf_init (&sb->residual,0 )); sb->residual_fully_formed =stream_buf_added (sb,0 );dmsg (D_STREAM_DEBUG ,"STREAM: RESIDUAL FULLY FORMED [%s], len=%d" , - stream_buf->residual_fully_formed ?"YES" :"NO" , sb->residual.len); + sb->residual_fully_formed ?"YES" :"NO" , sb->residual.len); } -if (!sb->residual_fully_formed) - { -stream_buf_set_next (sb); - }return !sb->residual_fully_formed; } +/** +* This will determine if \c sb->buf contains a full packet. It will also +* move anything in \c sb->buf beyond a full packet to \c sb->residual. +* +* The first time the function is called with a valid buffer and port sharing +* is enabled, the function will also determine if the buffer contains +* OpenVPN protocol data and store the result in \c sb->port_share_state. +* +* If a packet outside the allowed range is detected, the error state +* on \c sb is set. +* +* Since the buffer in \c sb->buf is modified from the outside (via +* \c stream_buf_get_next) the parameter \p length_added needs to be set +* to the amount of bytes that have been written to this buffer. If the +* buffer was not modified but should still be analysed and potentially +* split to \c sb->residual, the parameter \p length_added should be 0. +* +* @param sb the stream buffer +* @param length_added The length that has been added to \c sb->buf +* @return true if \c sb->buf contains fully reassembled packet +*/ static bool stream_buf_added (struct stream_buf *sb,int length_added) {
@@ -2203,7 +2232,6 @@else {dmsg (D_STREAM_DEBUG ,"STREAM: ADD returned FALSE (have=%d need=%d)" , sb->buf.len, sb->len); -stream_buf_set_next (sb);return false; } }
@@ -2273,8 +2301,7 @@sockethandle_t sh = { .s = sock->sd }; len =sockethandle_finalize (sh, &sock->reads, buf,NULL );#else -struct buffer frag; -stream_buf_get_next (&sock->stream_buf, &frag); +struct buffer frag =stream_buf_get_next (&sock->stream_buf); len =recv (sock->sd,BPTR (&frag),BLEN (&frag),MSG_NOSIGNAL );#endif
@@ -2550,7 +2577,7 @@ }else if (proto_is_tcp (sock->info.proto)) { -stream_buf_get_next (&sock->stream_buf, &sock->reads.buf); + sock->reads.buf =stream_buf_get_next (&sock->stream_buf); }else {
src/openvpn/socket.h +32 -9
64 lines not on this page · raw message
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 5
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: flichtenheld <[email protected]...>
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archiveAttention is currently required from: plaisthos.
flichtenheld has posted comments on this change by plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email )
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Patch Set 7: Code-Review+2
(1 comment)
Commit Message:
http://gerrit.openvpn.net/c/openvpn/+/1477/comment/c6179746_210069f4?usp=email :
PS7, Line 9: The stream_buf_set_next prepares a buffer in the stream_buf Either remove "The" or add "function"
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 7
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Comment-Date: Wed, 11 Feb 2026 15:25:35 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
PATCH v7 1/1 Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archiveFrom: Arne Schwabe <[email protected]...>
The stream_buf_set_next prepares a buffer in the stream_buf structure that will be retrieved by stream_buf_get the next time it is used.
This temporary copy of the buffer is unnecessary as the buffer next can also be constructed on the fly.
This also fixes a rare crash when read buffer are not initialised and read is still signalled as the initialisation of next will now happen whenever it is required.
This assertion happens when we do not expect a read event from the socket and then in link_socket_read_tcp the function stream_buf_get_next can trigger an assert on ASSERT(buf_defined(&sb->next));
To avoid this weird corner case, just always initialise the read buffer whether or not we expect a read to occur.
This also adds documentation about the methods and field associated with the stream_buf structure.
Reproducing this bug requires very special circumstances. The reproduction is
openvpn --cert server-secp384r1.crt --key server-secp384r1.key \
--dev ml-tan --dev-type tap --tun-mtu 1400 --config ~/fp --topology subnet \
--port 2201 --verb 3 --mode server --tls-server --proto tcp6-server --ifconfig 10.0.0.1 255.255.255.0
as the server side and
openvpn --client --proto tcp --remote poseidon --port 2201 --dev tap
The client side must be on Linux. Other platforms do not reproduce this bug.
Note that the client will not configure any IP or IPv6 on the interface and will also not bring up the interface. The server must also send at least one real data packet to the client (no keepalive ping). Just having the interface up normally produces enough traffic.
Now forcefully reset the TCP connection. E.g. by executing on the server
sudo ss --kill sport 2201
This will now trigger the assertion. This happens since OpenVPN waits forever to get a write back from the poll from the tun/tap device but this never happens since the device is not up.
As long as we do not get back the tun device for writing, we also do not put the socket back into the EVENT_READ state. And this also means that code to initialise the read buffer (stream_buf_set_next) is never run.
But the reset on the TCP socket triggers the TCP socket to be available for read, even if it is just for a read of 0 bytes to indicate the reset. So the function link_socket_read_tcp will run into the assert.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1477
---
This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1477
This mail reflects revision 7 of this Change.
Signed-off-by line for the author was added as per our policy.
Acked-by according to Gerrit (reflected above):
Frank Lichtenheld <[email protected]...>
src/openvpn/socket.c +56 -29
@@ -44,9 +44,7 @@bool sockets_read_residual (const struct context *c) { -int i; - -for (i =0 ; i < c->c1.link_sockets_num; i++) +for (int i =0 ; i < c->c1.link_sockets_num; i++) {if (c->c2.link_sockets[i]->stream_buf.residual_fully_formed) {
@@ -2053,13 +2051,19 @@* stream connection. */ +/** +* resets the stream buffer to be set up for the next round of +* reassembling a packet +* +* But still leaves the current packet in \c sb->buf to be potentially +* read. +*/ static inline void stream_buf_reset (struct stream_buf *sb) {dmsg (D_STREAM_DEBUG ,"STREAM: RESET" ); sb->residual_fully_formed = false; sb->buf = sb->buf_init; -buf_reset (&sb->next); sb->len =-1 ; }
@@ -2081,19 +2085,35 @@dmsg (D_STREAM_DEBUG ,"STREAM: INIT maxlen=%d" , sb->maxlen); } -static inline void -stream_buf_set_next (struct stream_buf *sb) +/** +* Return a buffer that is backed by the same backend as sb->buf that +* determines where the next read should be done by also having the +* right offset into \c sb->buf. +* @param sb the stream buffer from which to construct the next buffer +*/ +static inline struct buffer +stream_buf_get_next (struct stream_buf *sb) {/* set up 'next' for next i/o read */ - sb->next = sb->buf; - sb->next.offset = sb->buf.offset + sb->buf.len; - sb->next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; -dmsg (D_STREAM_DEBUG ,"STREAM: SET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , - sb->buf.offset, sb->buf.len, sb->next.offset, sb->next.len, sb->len, sb->maxlen); -ASSERT (sb->next.len >0 ); -ASSERT (buf_safe (&sb->buf, sb->next.len)); +struct buffer next; + next = sb->buf; + next.offset = sb->buf.offset + sb->buf.len; + next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; +dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , + sb->buf.offset, sb->buf.len, next.offset, next.len, sb->len, sb->maxlen); +ASSERT (next.len >0 ); +ASSERT (buf_safe (&sb->buf, next.len)); +return next; } +/** +* Sets the parameter buf to the current buffer of \c sb->buf. +* This function assumes that caller already checked if the packet in \c sb->buf +* is fully assembled. +* +* @param sb stream buffer to operate on +* @param buf buffer to point to the contents of buf +*/ static inline void stream_buf_get_final (struct stream_buf *sb,struct buffer *buf) {
@@ -2102,14 +2122,6 @@ *buf = sb->buf; } -static inline void -stream_buf_get_next (struct stream_buf *sb,struct buffer *buf) -{ -dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT len=%d" ,buf_defined (&sb->next) ? sb->next.len :-1 ); -ASSERT (buf_defined (&sb->next)); - *buf = sb->next; -} -bool stream_buf_read_setup_dowork (struct stream_buf *sb) {
@@ -2122,13 +2134,30 @@ sb->residual_fully_formed ? "YES" : "NO", sb->residual.len); } -if (!sb->residual_fully_formed) - { -stream_buf_set_next (sb); - }return !sb->residual_fully_formed; } +/** +* This will determine if \c sb->buf contains a full packet. It will also +* move anything in \c sb->buf beyond a full packet to \c sb->residual. +* +* The first time the function is called with a valid buffer and port sharing +* is enabled, the function will also determine if the buffer contains +* OpenVPN protocol data and store the result in \c sb->port_share_state. +* +* If a packet outside the allowed range is detected, the error state +* on \c sb is set. +* +* Since the buffer in \c sb->buf is modified from the outside (via +* \c stream_buf_get_next) the parameter \p length_added needs to be set +* to the amount of bytes that have been written to this buffer. If the +* buffer was not modified but should still be analysed and potentially +* split to \c sb->residual, the parameter \p length_added should be 0. +* +* @param sb the stream buffer +* @param length_added The length that has been added to \c sb->buf +* @return true if \c sb->buf contains fully reassembled packet +*/ static bool stream_buf_added (struct stream_buf *sb,int length_added) {
@@ -2191,7 +2220,6 @@else {dmsg (D_STREAM_DEBUG ,"STREAM: ADD returned FALSE (have=%d need=%d)" , sb->buf.len, sb->len); -stream_buf_set_next (sb);return false; } }
@@ -2261,8 +2289,7 @@sockethandle_t sh = { .s = sock->sd }; len =sockethandle_finalize (sh, &sock->reads, buf,NULL );#else -struct buffer frag; -stream_buf_get_next (&sock->stream_buf, &frag); +struct buffer frag =stream_buf_get_next (&sock->stream_buf); len =recv (sock->sd,BPTR (&frag),BLEN (&frag),MSG_NOSIGNAL );#endif
@@ -2538,7 +2565,7 @@ }else if (proto_is_tcp (sock->info.proto)) { -stream_buf_get_next (&sock->stream_buf, &sock->reads.buf); + sock->reads.buf =stream_buf_get_next (&sock->stream_buf); }else {
src/openvpn/socket.h +25 -7
@@ -95,23 +95,41 @@int mtu_changed;/* Set to true when mtu value is changed */ }; -/* -* Used to extract packets encapsulated in streams into a buffer, -* in this case IP packets embedded in a TCP stream. +/** +* struct used to extract packets encapsulated in streams into a buffer, +* in this case OpenVPN packets (data or control) embedded in a TCP stream. +* +* This struct is used to packetise the TCP stream into the +* OpenVPN packet. Each OpenVPN packet has a two-byte header determining +* the length of the packet. */ struct stream_buf { +/* Buffer to hold the initial buffer that will be used to reset buf */ struct buffer buf_init; + +/** buffer holding the excess bytes that are not part of the +* packet. */ struct buffer residual; + +/** Maximum length of a packet that we accept */ int maxlen; + +/** The buffer in buf contains a full packet without a header. Any +* extra data is in residual */ bool residual_fully_formed; +/** Holds the data of the current packet. This might be a partial packet */ struct buffer buf; -struct buffer next; -int len;/* -1 if not yet known */ -bool error;/* if true, fatal TCP error has occurred, -* requiring that connection be restarted */ +/** -1 if not yet known. Otherwise holds the length of the +* packet. If >= 0, buf is already moved past the initial +* size header */ +int len; + +/** if true, a fatal TCP error has occurred, +* requiring that connection be restarted */ +bool error;#if PORT_SHARE #define PS_DISABLED 0#define PS_ENABLED 1
[Openvpn-devel] [PATCH applied] Re: Merge stream_buf_get_next and stream_buf_set_next
So I managed to reproduce the original crash (--dev-tap --ifconfig-noexec, and then kill the socket), and can confirm that the patch fixes that.
I have also run this through the full client/server test bed, to verify that it's not breaking anything else in the process. Looks good (the patch looks bigger than it actually is, because "sb->next" is now "next" and that changes all lines in stream_buf_get_next() without a "real" change).
I have simplified the instructions in the commit message - it's not necessary to have a special server config, just "any TAP+TCP server" will do, with "--ifconfig-noexec" on the client side.
Your patch has been applied to the master & release/2.7 branch.
Backporting to 2.6 has not been done as the bug is considered too exotic to be a "real problem" in general (it's day 1 bug, so we *could* backport it all the way back to 2.2.x ;-) ).
commit 5e85c3491fcf75f1a006d410d1a2a7720c2d3f09 (master) commit 4d6cf3dcb821e281fdede8a989b50b9c1a2255c2 (release/2.7)
Author: Arne Schwabe Date: Mon Feb 16 17:22:31 2026 +0100
Merge stream_buf_get_next and stream_buf_set_next
Signed-off-by: Arne Schwabe <[email protected]...>
Acked-by: Frank Lichtenheld <[email protected]...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1477
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35673.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archivecron2 has uploaded a new patch set (#8) to the change originally created by plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email )
The following approvals got outdated and were removed:
Code-Review+2 by flichtenheld
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Merge stream_buf_get_next and stream_buf_set_next
The stream_buf_set_next prepares a buffer in the stream_buf structure that will be retrieved by stream_buf_get the next time it is used.
This temporary copy of the buffer is unnecessary as the buffer next can also be constructed on the fly.
This also fixes a rare crash when read buffer are not initialised and read is still signalled as the initialisation of next will now happen whenever it is required.
This assertion happens when we do not expect a read event from the socket and then in link_socket_read_tcp the function stream_buf_get_next can trigger an assert on ASSERT(buf_defined(&sb->next));
To avoid this weird corner case, just always initialise the read buffer whether or not we expect a read to occur.
This also adds documentation about the methods and field associated with the stream_buf structure.
Reproducing this bug requires very special circumstances. To reproduce, run a client with
openvpn --client --proto tcp --dev tap --ifconfig noexec ...
The client side must be on Linux. Other platforms do not reproduce this bug.
Note that the client will not configure any IP or IPv6 on the interface and will also not bring up the interface. The server must also send at least one real data packet to the client (no keepalive ping). Just having the interface up normally produces enough traffic.
Now forcefully reset the TCP connection. E.g. by executing on the client
sudo ss --kill dport <server port>
This will now trigger the assertion. This happens since OpenVPN waits forever to get a write back from the poll from the tun/tap device but this never happens since the device is not up.
As long as we do not get back the tun device for writing, we also do not put the socket back into the EVENT_READ state. And this also means that code to initialise the read buffer (stream_buf_set_next) is never run.
But the reset on the TCP socket triggers the TCP socket to be available for read, even if it is just for a read of 0 bytes to indicate the reset. So the function link_socket_read_tcp will run into the assert.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1477
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35673.html
---
M src/openvpn/socket.c
M src/openvpn/socket.h
2 files changed, 81 insertions(+), 36 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/77/1477/8
src/openvpn/socket.c +56 -29
@@ -44,9 +44,7 @@bool sockets_read_residual (const struct context *c) { -int i; - -for (i =0 ; i < c->c1.link_sockets_num; i++) +for (int i =0 ; i < c->c1.link_sockets_num; i++) {if (c->c2.link_sockets[i]->stream_buf.residual_fully_formed) {
@@ -2053,13 +2051,19 @@* stream connection. */ +/** +* resets the stream buffer to be set up for the next round of +* reassembling a packet +* +* But still leaves the current packet in \c sb->buf to be potentially +* read. +*/ static inline void stream_buf_reset (struct stream_buf *sb) {dmsg (D_STREAM_DEBUG ,"STREAM: RESET" ); sb->residual_fully_formed = false; sb->buf = sb->buf_init; -buf_reset (&sb->next); sb->len =-1 ; }
@@ -2081,19 +2085,35 @@dmsg (D_STREAM_DEBUG ,"STREAM: INIT maxlen=%d" , sb->maxlen); } -static inline void -stream_buf_set_next (struct stream_buf *sb) +/** +* Return a buffer that is backed by the same backend as sb->buf that +* determines where the next read should be done by also having the +* right offset into \c sb->buf. +* @param sb the stream buffer from which to construct the next buffer +*/ +static inline struct buffer +stream_buf_get_next (struct stream_buf *sb) {/* set up 'next' for next i/o read */ - sb->next = sb->buf; - sb->next.offset = sb->buf.offset + sb->buf.len; - sb->next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; -dmsg (D_STREAM_DEBUG ,"STREAM: SET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , - sb->buf.offset, sb->buf.len, sb->next.offset, sb->next.len, sb->len, sb->maxlen); -ASSERT (sb->next.len >0 ); -ASSERT (buf_safe (&sb->buf, sb->next.len)); +struct buffer next; + next = sb->buf; + next.offset = sb->buf.offset + sb->buf.len; + next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; +dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , + sb->buf.offset, sb->buf.len, next.offset, next.len, sb->len, sb->maxlen); +ASSERT (next.len >0 ); +ASSERT (buf_safe (&sb->buf, next.len)); +return next; } +/** +* Sets the parameter buf to the current buffer of \c sb->buf. +* This function assumes that caller already checked if the packet in \c sb->buf +* is fully assembled. +* +* @param sb stream buffer to operate on +* @param buf buffer to point to the contents of buf +*/ static inline void stream_buf_get_final (struct stream_buf *sb,struct buffer *buf) {
@@ -2102,14 +2122,6 @@ *buf = sb->buf; } -static inline void -stream_buf_get_next (struct stream_buf *sb,struct buffer *buf) -{ -dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT len=%d" ,buf_defined (&sb->next) ? sb->next.len :-1 ); -ASSERT (buf_defined (&sb->next)); - *buf = sb->next; -} -bool stream_buf_read_setup_dowork (struct stream_buf *sb) {
@@ -2122,13 +2134,30 @@ sb->residual_fully_formed ? "YES" : "NO", sb->residual.len); } -if (!sb->residual_fully_formed) - { -stream_buf_set_next (sb); - }return !sb->residual_fully_formed; } +/** +* This will determine if \c sb->buf contains a full packet. It will also +* move anything in \c sb->buf beyond a full packet to \c sb->residual. +* +* The first time the function is called with a valid buffer and port sharing +* is enabled, the function will also determine if the buffer contains +* OpenVPN protocol data and store the result in \c sb->port_share_state. +* +* If a packet outside the allowed range is detected, the error state +* on \c sb is set. +* +* Since the buffer in \c sb->buf is modified from the outside (via +* \c stream_buf_get_next) the parameter \p length_added needs to be set +* to the amount of bytes that have been written to this buffer. If the +* buffer was not modified but should still be analysed and potentially +* split to \c sb->residual, the parameter \p length_added should be 0. +* +* @param sb the stream buffer +* @param length_added The length that has been added to \c sb->buf +* @return true if \c sb->buf contains fully reassembled packet +*/ static bool stream_buf_added (struct stream_buf *sb,int length_added) {
@@ -2191,7 +2220,6 @@else {dmsg (D_STREAM_DEBUG ,"STREAM: ADD returned FALSE (have=%d need=%d)" , sb->buf.len, sb->len); -stream_buf_set_next (sb);return false; } }
@@ -2261,8 +2289,7 @@sockethandle_t sh = { .s = sock->sd }; len =sockethandle_finalize (sh, &sock->reads, buf,NULL );#else -struct buffer frag; -stream_buf_get_next (&sock->stream_buf, &frag); +struct buffer frag =stream_buf_get_next (&sock->stream_buf); len =recv (sock->sd,BPTR (&frag),BLEN (&frag),MSG_NOSIGNAL );#endif
@@ -2538,7 +2565,7 @@ }else if (proto_is_tcp (sock->info.proto)) { -stream_buf_get_next (&sock->stream_buf, &sock->reads.buf); + sock->reads.buf =stream_buf_get_next (&sock->stream_buf); }else {
src/openvpn/socket.h +25 -7
@@ -95,23 +95,41 @@int mtu_changed;/* Set to true when mtu value is changed */ }; -/* -* Used to extract packets encapsulated in streams into a buffer, -* in this case IP packets embedded in a TCP stream. +/** +* struct used to extract packets encapsulated in streams into a buffer, +* in this case OpenVPN packets (data or control) embedded in a TCP stream. +* +* This struct is used to packetise the TCP stream into the +* OpenVPN packet. Each OpenVPN packet has a two-byte header determining +* the length of the packet. */ struct stream_buf { +/* Buffer to hold the initial buffer that will be used to reset buf */ struct buffer buf_init; + +/** buffer holding the excess bytes that are not part of the +* packet. */ struct buffer residual; + +/** Maximum length of a packet that we accept */ int maxlen; + +/** The buffer in buf contains a full packet without a header. Any +* extra data is in residual */ bool residual_fully_formed; +/** Holds the data of the current packet. This might be a partial packet */ struct buffer buf; -struct buffer next; -int len;/* -1 if not yet known */ -bool error;/* if true, fatal TCP error has occurred, -* requiring that connection be restarted */ +/** -1 if not yet known. Otherwise holds the length of the +* packet. If >= 0, buf is already moved past the initial +* size header */ +int len; + +/** if true, a fatal TCP error has occurred, +* requiring that connection be restarted */ +bool error;#if PORT_SHARE #define PS_DISABLED 0#define PS_ENABLED 1
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 8
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
[Openvpn-devel] [M] Change in openvpn[master]: Merge stream_buf_get_next and stream_buf_set_next
In reply to a message not in the archivecron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email )
Change subject: Merge stream_buf_get_next and stream_buf_set_next
......................................................................
Merge stream_buf_get_next and stream_buf_set_next
The stream_buf_set_next prepares a buffer in the stream_buf structure that will be retrieved by stream_buf_get the next time it is used.
This temporary copy of the buffer is unnecessary as the buffer next can also be constructed on the fly.
This also fixes a rare crash when read buffer are not initialised and read is still signalled as the initialisation of next will now happen whenever it is required.
This assertion happens when we do not expect a read event from the socket and then in link_socket_read_tcp the function stream_buf_get_next can trigger an assert on ASSERT(buf_defined(&sb->next));
To avoid this weird corner case, just always initialise the read buffer whether or not we expect a read to occur.
This also adds documentation about the methods and field associated with the stream_buf structure.
Reproducing this bug requires very special circumstances. To reproduce, run a client with
openvpn --client --proto tcp --dev tap --ifconfig noexec ...
The client side must be on Linux. Other platforms do not reproduce this bug.
Note that the client will not configure any IP or IPv6 on the interface and will also not bring up the interface. The server must also send at least one real data packet to the client (no keepalive ping). Just having the interface up normally produces enough traffic.
Now forcefully reset the TCP connection. E.g. by executing on the client
sudo ss --kill dport <server port>
This will now trigger the assertion. This happens since OpenVPN waits forever to get a write back from the poll from the tun/tap device but this never happens since the device is not up.
As long as we do not get back the tun device for writing, we also do not put the socket back into the EVENT_READ state. And this also means that code to initialise the read buffer (stream_buf_set_next) is never run.
But the reset on the TCP socket triggers the TCP socket to be available for read, even if it is just for a read of 0 bytes to indicate the reset. So the function link_socket_read_tcp will run into the assert.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1477
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg35673.html
---
M src/openvpn/socket.c
M src/openvpn/socket.h
2 files changed, 81 insertions(+), 36 deletions(-)
src/openvpn/socket.c +56 -29
@@ -44,9 +44,7 @@bool sockets_read_residual (const struct context *c) { -int i; - -for (i =0 ; i < c->c1.link_sockets_num; i++) +for (int i =0 ; i < c->c1.link_sockets_num; i++) {if (c->c2.link_sockets[i]->stream_buf.residual_fully_formed) {
@@ -2053,13 +2051,19 @@* stream connection. */ +/** +* resets the stream buffer to be set up for the next round of +* reassembling a packet +* +* But still leaves the current packet in \c sb->buf to be potentially +* read. +*/ static inline void stream_buf_reset (struct stream_buf *sb) {dmsg (D_STREAM_DEBUG ,"STREAM: RESET" ); sb->residual_fully_formed = false; sb->buf = sb->buf_init; -buf_reset (&sb->next); sb->len =-1 ; }
@@ -2081,19 +2085,35 @@dmsg (D_STREAM_DEBUG ,"STREAM: INIT maxlen=%d" , sb->maxlen); } -static inline void -stream_buf_set_next (struct stream_buf *sb) +/** +* Return a buffer that is backed by the same backend as sb->buf that +* determines where the next read should be done by also having the +* right offset into \c sb->buf. +* @param sb the stream buffer from which to construct the next buffer +*/ +static inline struct buffer +stream_buf_get_next (struct stream_buf *sb) {/* set up 'next' for next i/o read */ - sb->next = sb->buf; - sb->next.offset = sb->buf.offset + sb->buf.len; - sb->next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; -dmsg (D_STREAM_DEBUG ,"STREAM: SET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , - sb->buf.offset, sb->buf.len, sb->next.offset, sb->next.len, sb->len, sb->maxlen); -ASSERT (sb->next.len >0 ); -ASSERT (buf_safe (&sb->buf, sb->next.len)); +struct buffer next; + next = sb->buf; + next.offset = sb->buf.offset + sb->buf.len; + next.len = (sb->len >=0 ? sb->len : sb->maxlen) - sb->buf.len; +dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d" , + sb->buf.offset, sb->buf.len, next.offset, next.len, sb->len, sb->maxlen); +ASSERT (next.len >0 ); +ASSERT (buf_safe (&sb->buf, next.len)); +return next; } +/** +* Sets the parameter buf to the current buffer of \c sb->buf. +* This function assumes that caller already checked if the packet in \c sb->buf +* is fully assembled. +* +* @param sb stream buffer to operate on +* @param buf buffer to point to the contents of buf +*/ static inline void stream_buf_get_final (struct stream_buf *sb,struct buffer *buf) {
@@ -2102,14 +2122,6 @@ *buf = sb->buf; } -static inline void -stream_buf_get_next (struct stream_buf *sb,struct buffer *buf) -{ -dmsg (D_STREAM_DEBUG ,"STREAM: GET NEXT len=%d" ,buf_defined (&sb->next) ? sb->next.len :-1 ); -ASSERT (buf_defined (&sb->next)); - *buf = sb->next; -} -bool stream_buf_read_setup_dowork (struct stream_buf *sb) {
@@ -2122,13 +2134,30 @@ sb->residual_fully_formed ? "YES" : "NO", sb->residual.len); } -if (!sb->residual_fully_formed) - { -stream_buf_set_next (sb); - }return !sb->residual_fully_formed; } +/** +* This will determine if \c sb->buf contains a full packet. It will also +* move anything in \c sb->buf beyond a full packet to \c sb->residual. +* +* The first time the function is called with a valid buffer and port sharing +* is enabled, the function will also determine if the buffer contains +* OpenVPN protocol data and store the result in \c sb->port_share_state. +* +* If a packet outside the allowed range is detected, the error state +* on \c sb is set. +* +* Since the buffer in \c sb->buf is modified from the outside (via +* \c stream_buf_get_next) the parameter \p length_added needs to be set +* to the amount of bytes that have been written to this buffer. If the +* buffer was not modified but should still be analysed and potentially +* split to \c sb->residual, the parameter \p length_added should be 0. +* +* @param sb the stream buffer +* @param length_added The length that has been added to \c sb->buf +* @return true if \c sb->buf contains fully reassembled packet +*/ static bool stream_buf_added (struct stream_buf *sb,int length_added) {
@@ -2191,7 +2220,6 @@else {dmsg (D_STREAM_DEBUG ,"STREAM: ADD returned FALSE (have=%d need=%d)" , sb->buf.len, sb->len); -stream_buf_set_next (sb);return false; } }
@@ -2261,8 +2289,7 @@sockethandle_t sh = { .s = sock->sd }; len =sockethandle_finalize (sh, &sock->reads, buf,NULL );#else -struct buffer frag; -stream_buf_get_next (&sock->stream_buf, &frag); +struct buffer frag =stream_buf_get_next (&sock->stream_buf); len =recv (sock->sd,BPTR (&frag),BLEN (&frag),MSG_NOSIGNAL );#endif
@@ -2538,7 +2565,7 @@ }else if (proto_is_tcp (sock->info.proto)) { -stream_buf_get_next (&sock->stream_buf, &sock->reads.buf); + sock->reads.buf =stream_buf_get_next (&sock->stream_buf); }else {
src/openvpn/socket.h +25 -7
@@ -95,23 +95,41 @@int mtu_changed;/* Set to true when mtu value is changed */ }; -/* -* Used to extract packets encapsulated in streams into a buffer, -* in this case IP packets embedded in a TCP stream. +/** +* struct used to extract packets encapsulated in streams into a buffer, +* in this case OpenVPN packets (data or control) embedded in a TCP stream. +* +* This struct is used to packetise the TCP stream into the +* OpenVPN packet. Each OpenVPN packet has a two-byte header determining +* the length of the packet. */ struct stream_buf { +/* Buffer to hold the initial buffer that will be used to reset buf */ struct buffer buf_init; + +/** buffer holding the excess bytes that are not part of the +* packet. */ struct buffer residual; + +/** Maximum length of a packet that we accept */ int maxlen; + +/** The buffer in buf contains a full packet without a header. Any +* extra data is in residual */ bool residual_fully_formed; +/** Holds the data of the current packet. This might be a partial packet */ struct buffer buf; -struct buffer next; -int len;/* -1 if not yet known */ -bool error;/* if true, fatal TCP error has occurred, -* requiring that connection be restarted */ +/** -1 if not yet known. Otherwise holds the length of the +* packet. If >= 0, buf is already moved past the initial +* size header */ +int len; + +/** if true, a fatal TCP error has occurred, +* requiring that connection be restarted */ +bool error;#if PORT_SHARE #define PS_DISABLED 0#define PS_ENABLED 1
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1477?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifd3e953104a67c8bf2a225e179865e3dbd0dbfbc
Gerrit-Change-Number: 1477
Gerrit-PatchSet: 8
Gerrit-Owner: plaisthos <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>